import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//@WebServlet("/login")
public class Cusfilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        String username=servletRequest.getParameter("username").trim();
        String password=servletRequest.getParameter("password").trim();
        System.out.printf("username:{%s}------------password:{%s}\n",username,password);
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpServletResponse resp=(HttpServletResponse) servletResponse;
        HttpSession session=req.getSession();
        Integer uid=(Integer)session.getAttribute("userid");
        System.out.println("uid:"+uid);
        String path=req.getRequestURI();
        if (username.equals("admin") && password.equals("123123")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else {
            resp.sendRedirect("abandon.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
